package cc.admin.modules.sys.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.aspect.annotation.AutoLog;
import cc.admin.common.sys.base.controller.BaseController;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.common.sys.vo.LoginUser;
import cc.admin.modules.sys.entity.SysCompany;
import cc.admin.modules.sys.entity.SysUser;
import cc.admin.modules.sys.service.ISysCompanyService;
import cc.admin.modules.sys.service.ISysUserService;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 动态表单
 * @Author: cc-admin
 * @Date: 2021-11-17
 * @Version: V1.0.0
 */
@Slf4j
@Api(tags = "动态表单")
@RestController
@RequestMapping("/sys/company")
public class SysCompanyController extends BaseController<SysCompany, ISysCompanyService> {
    @Autowired
    private ISysCompanyService sysCompanyService;

	@Autowired
	private ISysUserService sysUserService;
    /**
     * 分页列表查询
     *
     * @param sysCompany
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "动态表单-分页列表查询")
    @ApiOperation(value = "动态表单-分页列表查询", notes = "动态表单-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
            SysCompany sysCompany,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        QueryWrapper<SysCompany> queryWrapper = QueryGenerator.initQueryWrapper(sysCompany, req.getParameterMap());

        Page<SysCompany> page = new Page<SysCompany>(pageNo, pageSize);
        IPage<SysCompany> pageList = sysCompanyService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param sysCompany
     * @return
     */
    @AutoLog(value = "动态表单-添加")
    @ApiOperation(value = "动态表单-添加", notes = "动态表单-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SysCompany sysCompany) {
        sysCompanyService.addCompany(sysCompany);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sysCompany
     * @return
     */
    @AutoLog(value = "动态表单-编辑")
    @ApiOperation(value = "动态表单-编辑", notes = "动态表单-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SysCompany sysCompany) {
        sysCompanyService.updateById(sysCompany);
        return Result.ok("编辑成功!");
    }


    /**
     * 复制
     *
     * @param id
     * @param name
     * @return
     */
    @AutoLog(value = "动态表单-复制")
    @ApiOperation(value = "动态表单-复制", notes = "动态表单-复制")
    @PostMapping(value = "/copy")
    public Result<?> copy(@RequestParam(name = "id", required = true) String id, @RequestParam(name = "name", required = true) String name) {
        SysCompany sysCompany = sysCompanyService.getById(id);
        sysCompany.setId(IdUtil.fastUUID());
        sysCompany.setName(name);
        sysCompanyService.save(sysCompany);
        return Result.ok("复制成功！");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "动态表单-通过id删除")
    @ApiOperation(value = "动态表单-通过id删除", notes = "动态表单-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
//        sysCompanyService.removeById(id);
        sysCompanyService.deleteCompany(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "动态表单-批量删除")
    @ApiOperation(value = "动态表单-批量删除", notes = "动态表单-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.sysCompanyService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "动态表单-通过id查询")
    @ApiOperation(value = "动态表单-通过id查询", notes = "动态表单-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        SysCompany sysCompany = sysCompanyService.getById(id);
        return Result.ok(sysCompany);
    }


	/**
	 * 获取当前租户信息
	 *
	 * @return
	 */
	@AutoLog(value = "动态表单-获取当前租户信息")
	@ApiOperation(value = "动态表单-获取当前租户信息", notes = "动态表单-获取当前租户信息")
	@GetMapping(value = "/getTenantInfo")
	public Result<?> getTenantInfo(@RequestParam(name = "username", required = true) String username) {
		SysUser sysUser = sysUserService.getUserByName(username);
		SysCompany sysCompany = sysCompanyService.getById(sysUser.getTenantId());
		return Result.ok(sysCompany);
	}


    /**
     * 导出excel
     *
     * @param request
     * @param sysCompany
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysCompany sysCompany) {
        return super.exportXls(request, sysCompany, SysCompany.class, "动态表单");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysCompany.class);
    }

    /**
     * 公司下拉
     * @return
     */
    @RequestMapping(value = "/getCompanyName", method = RequestMethod.GET)
    public List<Map<String,String>> getCompanyName(){
        return sysCompanyService.getCompanyName();
    }

	/**
	 * 公司下拉
	 * @return
	 */
	@RequestMapping(value = "/getSignType", method = RequestMethod.GET)
	public String getSignType(){
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		SysCompany sysCompany = sysCompanyService.getById(sysUser.getTenantId());
		String signType = sysCompany.getSignType();
		if (StrUtil.isEmpty(signType)) {
			signType = "mouse";
		}
		return signType;
	}

}
